using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using UMC.Data;
using UMC.Data.Entities;
using UMC.ITME.Entities;
using UMC.Net;
using UMC.Web;

namespace UMC.ITME.Activities
{

    [UMC.Web.Apiumc("ITME", "Firewall", Auth = WebAuthType.Admin, Desc = "防火墙")]
    class SiteFirewallActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var uTitle = new UITitle("防火墙");
            var ui = UISection.Create(uTitle);
            var Fingerprint = this.AsyncDialog("Fingerprint", "0");
            var ui2 = ui;

            var model = this.AsyncDialog("Model", r =>
            {
                var start = Utility.IntParse(this.AsyncDialog("start", "0"), 0);
                if (Fingerprint.Length > 1 && start == 0)
                {
                    if (System.Net.IPAddress.TryParse(Fingerprint, out var address))
                    {
                        ui.AddCell("IP地址", address.ToString());
                        var ips = new byte[16];
                        address.TryWriteBytes(ips, out var L);
                        var frie = DataFactory.Instance().Firewall(new Net.StringValue(ips, 0, L));
                        switch (frie?.AuthType ?? AuthType.Check)
                        {
                            case AuthType.Check:
                                ui.AddCell("访问许可", "检验");
                                ui.UIFootBar = new UIFootBar().AddText(
               new UIEventText("允许访问").Click(new UIClick(request, r, "Allow")
                ).Style(new UIStyle().BgColor()), new UIEventText("拒绝访问").Click(new UIClick(request, r, "Deny")).Style(new UIStyle().Color(0xfff).BgColor(0xff4949)));
                                ui.UIFootBar.IsFixed = true;
                                break;
                            case AuthType.Allow:

                                ui.AddCell("访问许可", "允许");

                                ui.UIFootBar = new UIFootBar().AddText(
               new UIEventText("检验访问").Click(new UIClick(request, r, "Check")
                ).Style(new UIStyle().BgColor()), new UIEventText("拒绝访问").Click(new UIClick(request, r, "Deny")).Style(new UIStyle().Color(0xfff).BgColor(0xff4949)));
                                ui.UIFootBar.IsFixed = true;
                                break;
                            case AuthType.Deny:
                                ui.AddCell("访问许可", "拒绝");

                                ui.UIFootBar = new UIFootBar()
                                .AddText(new UIEventText("允许访问").Click(new UIClick(request, r, "Allow")).Style(new UIStyle().BgColor()), new UIEventText("检验访问").Click(new UIClick(request, r, "Check")).Style(new UIStyle().Color(0xfff).BgColor(0xff4949)));
                                ui.UIFootBar.IsFixed = true;
                                break;

                        }
                        ui.AddCell("终端名称", frie?.Caption?.UTF8(), new UIClick(request, r, "Caption"));

                    }
                    else
                    {
                        ui.AddCell("应用指纹", Fingerprint);

                        var bytes = Utility.FromBase64String(Fingerprint);
                        var frie = DataFactory.Instance().Firewall(new Net.StringValue(bytes));
                        switch (frie?.AuthType ?? AuthType.Check)
                        {
                            case AuthType.Check:
                                ui.AddCell("访问许可", "检验");

                                ui.UIFootBar = new UIFootBar().AddText(
               new UIEventText("允许访问").Click(new UIClick(request, r, "Allow")
                ).Style(new UIStyle().BgColor()), new UIEventText("拒绝访问").Click(new UIClick(request, r, "Deny")).Style(new UIStyle().Color(0xfff).BgColor(0xff4949)));
                                ui.UIFootBar.IsFixed = true;
                                break;
                            case AuthType.Allow:

                                ui.AddCell("访问许可", "允许");

                                ui.UIFootBar = new UIFootBar().AddText(
               new UIEventText("检验访问").Click(new UIClick(request, r, "Check")
                ).Style(new UIStyle().BgColor()), new UIEventText("拒绝访问").Click(new UIClick(request, r, "Deny")).Style(new UIStyle().Color(0xfff).BgColor(0xff4949)));
                                ui.UIFootBar.IsFixed = true;
                                break;
                            case AuthType.Deny:
                                ui.AddCell("访问许可", "拒绝");
                                ui.UIFootBar = new UIFootBar().AddText(
               new UIEventText("允许访问").Click(new UIClick(request, r, "Allow")
                ).Style(new UIStyle().BgColor()), new UIEventText("检验访问").Click(new UIClick(request, r, "Check")).Style(new UIStyle().Color(0xfff).BgColor(0xff4949)));
                                ui.UIFootBar.IsFixed = true;
                                break;

                        }
                        ui.AddCell("终端名称", frie?.Caption?.UTF8(), new UIClick(request, r, "Caption"));
                    }
                    ui2 = ui.NewSection();
                }
                var fires = HotCache.Find(new Firewall(), false, start, 100, out int nextIndex);
                ui.StartIndex = nextIndex;
                ui2.Key = "Firewall";
                ui2.IsNext = nextIndex > -1;
                foreach (var fire in fires)
                {
                    if (fire.Type == 0)
                    {
                        var address = new System.Net.IPAddress(fire.Fingerprint.Span).ToString();
                        if (address == Fingerprint)
                        {
                            continue;
                        }
                        switch (fire?.AuthType ?? AuthType.Check)
                        {
                            case AuthType.Check:
                                ui2.AddCell(address, "检验");
                                break;
                            case AuthType.Allow:

                                ui2.AddCell(address, "允许", UIClick.Query(new WebMeta("Fingerprint", address)));
                                break;
                            case AuthType.Deny:
                                ui2.AddCell(address, "拒绝", UIClick.Query(new WebMeta("Fingerprint", address)));
                                break;

                        }
                    }
                    else
                    {
                        var finger = Utility.ToBase64String(fire.Fingerprint.Span);
                        if (finger == Fingerprint)
                        {
                            continue;
                        }
                        switch (fire?.AuthType ?? AuthType.Check)
                        {
                            case AuthType.Check:
                                ui2.AddCell(finger, "检验", UIClick.Query(new WebMeta("Fingerprint", finger)));
                                break;
                            case AuthType.Allow:

                                ui2.AddCell(finger, "允许", UIClick.Query(new WebMeta("Fingerprint", finger)));
                                break;
                            case AuthType.Deny:
                                ui2.AddCell(finger, "拒绝", UIClick.Query(new WebMeta("Fingerprint", finger)));
                                break;

                        }
                    }
                    // if()
                }



                ui.SendTo(this.Context, $"{request.Model}.{request.Command}");

            });
            int type = 0;
            Net.StringValue stringValue = Net.StringValue.Empty;
            if (Fingerprint.Length > 1)
            {
                if (System.Net.IPAddress.TryParse(Fingerprint, out var address))
                {
                    var ips = new byte[16];
                    address.TryWriteBytes(ips, out var L);
                    stringValue = new Net.StringValue(ips, 0, L);
                }
                else
                {
                    type = 1;
                    stringValue = new Net.StringValue(Utility.FromBase64String(Fingerprint));
                }

            }
            switch (model)
            {
                case "Caption":
                    var caption = this.AsyncDialog("Caption", r => new UITextDialog() { Title = "终端名称", DefaultValue = DataFactory.Instance().Firewall(stringValue)?.Caption?.UTF8() });

                    HotCache.Put(new Firewall { Fingerprint = stringValue, Caption = new StringValue(caption) });
                    break;
                case "Allow":
                    this.Context.Loger.Info().Write("设置了允许").Write(Fingerprint).Write("访问").End();
                    HotCache.Put(new Firewall { Fingerprint = stringValue, AuthType = AuthType.Allow, Type = type });
                    Utility.SyncClient(4, stringValue, new StringValue(new byte[] { 1 }));

                    break;
                case "Deny":
                    if (type == 0 && Fingerprint == request.UserHostAddress)
                    {
                        this.Prompt("不能设置拒绝当前自己IP");
                    }
                    this.Context.Loger.Info().Write("设置了拒绝").Write(Fingerprint).Write("访问").End();
                    HotCache.Put(new Firewall { Fingerprint = stringValue, AuthType = AuthType.Deny, Type = type });
                    Utility.SyncClient(4, stringValue, new StringValue(new byte[] { 2 }));
                    // Async(stringValue, 2);
                    break;
                case "Check":
                    this.Context.Loger.Info().Write("还原了").Write(Fingerprint).Write("访问").End();
                    HotCache.Delete(new Firewall { Fingerprint = stringValue });
                    // Async(stringValue, 0);
                    Utility.SyncClient(4, stringValue);// new StringValue(new byte[] { 1 }));

                    break;
            }
            this.Context.Send(true);
        }
    }
}